<template>
    <div>
        <h4>发表评论</h4>
        <hr>
        <textarea placeholder="请输入想说的。。。(最多吐槽120字)" maxlength="120" v-model="message"></textarea>
        <mt-button type="primary" size="large" @click="postComment()">发表评论</mt-button>
        <div class="cmt-container">
            <div class="cmt-content" v-for="(item,index) in comments" :key="item.user_name">
                <div class="cmt-title">
                    第{{index+1}}楼&nbsp;&nbsp;用户:{{item.user_name}}&nbsp;&nbsp;发表时间:{{item.add_time}}
                </div>
                <div class="cmt-body">
                    {{item.content}}
                </div>
            </div>
        </div>
        
        <mt-button type="danger" size="large" plain @click="getMore()">加载更多</mt-button>
    </div>
</template>

<script>
import { Toast } from 'mint-ui';
export default {
    props:["id"],
    data(){
        return {
            comments:[],
            index: 1,
            message: ""
        }
    },
    created(){
        console.log(this.id);
        this.getComments();
    },
    methods:{
        getComments(){
            var url = "../../json/comment" + this.index + ".json";
            if(this.index <=2){
                this.$axios.get(url).then(res=>{
                //console.log(res.data);
                this.comments = this.comments.concat(res.data);
            })
            }
            
            //console.log(this.comments);
        },
        getMore(){
            this.index++;
            this.getComments();
        },
        postComment(){
            if(this.message.trim().length === 0){
                return Toast("评论不能为空");
            }else{
                //拼接一个对象
                var cmt = {
                    "user_name": "匿名用户",
                    "add_time": Date.now(),
                    "content": this.message.trim()
                };
                this.comments.unshift(cmt);
                this.message = "";
            }
        }
    }
}
</script>

<style scoped>
    h4{
        font-size: 18px;
        text-align: left;
    }
    textarea{
        font-size: 13px;
        height: 85px;
        margin: 0;
    }
    .cmt-container {
        margin: 5px 0;
    }
    .cmt-content{
        font-size: 13px;
    }
    .cmt-title{
        background-color: #ccc;
    }
    .cmt-body{
        text-indent: 2em;
    }
</style>